<?php

/**
* alltosun.com 终端设备控制器 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 刘德洪 (Liudh@alltosun.com) $
* $Date: 20151124 $
* $Id$
*/

class Action
{
    private $per_page = 20;

    public function __construct()
    {

    }

     /**
     * 显示订单列表
     */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::getParam('status', 1);
        $progress      = Request::getParam('progress', '-1');
        $search_filter = Request::getParam('search_filter', array());

        // 拼装搜索条件
        $is_empty = 0;
        $filter = array();
        if (isset($search_filter['id']) && $search_filter['id']) {
            $filter['id'] = $search_filter['id'];
        }
        if (isset($search_filter['progress']) && $search_filter['progress'] && $search_filter['progress'] != '-1') {
            $filter['progress'] = $search_filter['progress'];
        }
        if (isset($search_filter['order_code']) && $search_filter['order_code']) {
            $decoded_id = order_helper::order_id_decode($search_filter['order_code']);
            if(is_numeric($decoded_id)) {
                $filter['id'] = $decoded_id;
            }
        }

        if (isset($search_filter['goods_title']) && $search_filter['goods_title']) {
            //通过快照查询order_ids
            $snapshot_filter['`goods_title` LIKE '] = '%'.$search_filter['goods_title'].'%';
            $order_ids = _model('sku_snapshot')->getFields('order_id', $snapshot_filter);
            if ($order_ids) {
                $filter['id'] = $order_ids;
            } else {
                $is_empty = 1;
            }
        }
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }
        if (isset($progress) && $progress != '-1') {
            $filter['progress'] = intval($progress);
        }
        if ($progress != '-1' || $status == 0) {
            $filter['status'] = $status;
        } else {
            $filter['1'] = 1;
        }

        $order_list = array();
        if (!$is_empty) {
            $order_count = _model('order')->getTotal($filter);
            if ($order_count) {
                $pager = new Pager($this->per_page);
                if ($pager->generate($order_count)) {
                    Response::assign('pager', $pager);
                }
                $order_list = _model('order')->getList($filter, ' ORDER BY `add_time` DESC '.$pager->getLimit($page));
                foreach ($order_list as $k=>$v) {
                    $order_list[$k]['order_relation'] = _model('order_relation')->getList(array(
                                    'order_id'=>$v['id'],
                                    'status'  => 1
                    ));
                }

                Response::assign('count', $order_count);
            }
        }

        $express_list = _model('express')->getList(array('status'=>1));

        Response::assign('express_list', $express_list);
        Response::assign('order_list', $order_list);
        Response::assign('status', $status);
        Response::assign('progress', $progress);
        Response::assign('search_filter', $search_filter);

        Response::display('admin/order_list.html');
    }

}
